package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.bean.ApiInfo;
import com.bean.LoginCreds;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.github.pagehelper.PageInfo;
import com.service.impl.ApiInfoService;
import com.service.impl.LoginCredsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;

@RestController
@CrossOrigin
@RequestMapping("api/api_info")
@Slf4j
public class ApiInfoController {

    @Autowired
    private ApiInfoService apiInfoService;

    @PostMapping("add")
    public ResponseEntity<ResponseData> add(@RequestBody ApiInfo apiInfo){
        apiInfoService.insert(apiInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

    @PostMapping("edit")
    public ResponseEntity<ResponseData> edit(@RequestBody ApiInfo apiInfo){
        apiInfoService.update(apiInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("del/{id}")
    public ResponseEntity<ResponseData> del(@PathVariable String id){
        ApiInfo apiInfo=new ApiInfo();
        apiInfo.setId(id);
        apiInfoService.delete(apiInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("get")
    public ResponseEntity<ResponseData> get(@RequestParam("id")String id){
        ApiInfo apiInfo=apiInfoService.get(id);
        return ResponseEntity.ok(ResponseData.success(apiInfo));
    }

    @PostMapping("list")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> list(@RequestBody RequestData<ApiInfo> requestData){
        ApiInfo apiInfo=requestData.getObj();
        apiInfo.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<ApiInfo> pageInfo=apiInfoService.select(apiInfo,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }

    //IP定位
    @GetMapping("ipdw")
    public ResponseEntity<JSONObject> ipdw(HttpServletRequest request){
        String ip =request.getRemoteHost();
        System.out.println(ip);
        System.out.println(request.getRemoteAddr());
        System.out.println(request.getRemoteUser());
        return ResponseEntity.ok(apiInfoService.ApiGetIpdw(ip));
    }

    //IP定位
    @GetMapping("ipdw2")
    public ResponseEntity<JSONObject> ipdw2(HttpServletRequest request){
        String ip =request.getRemoteHost();
        System.out.println(ip);
        System.out.println(request.getRemoteAddr());
        System.out.println(request.getRemoteUser());
        return ResponseEntity.ok(apiInfoService.ApiGetIpdw2(ip));
    }

    //实时天气   需要所在的城市名称
    @GetMapping("sstq")
    public ResponseEntity<JSONObject> sstq(@Param("location")String location){
        log.info("实时天气的地址为:"+location);
        return ResponseEntity.ok(apiInfoService.ApiGetSstq(location));
    }

    //每日搞笑段子
    @GetMapping("gxdz")
    public ResponseEntity<JSONObject> gxdz(){
        return ResponseEntity.ok(apiInfoService.ApiGetGxdz());
    }


    //菜谱
    @PostMapping("cp")
    public ResponseEntity<JSONObject> cp(){
        return ResponseEntity.ok(apiInfoService.ApiGetGxdz());
    }


    //网易云随机音乐
    @GetMapping("sjmusic")
    public ResponseEntity<JSONObject> sjmusic(@RequestParam("type") String type){
        log.info("type"+type);
        return ResponseEntity.ok(apiInfoService.sjmusic(type));
    }

}
